import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import hashlib

with open('h5st.js', 'r', encoding='utf-8') as f:
    js = f.read()

js_com = execjs.compile(js)


def sha_256(message):
    return hashlib.sha256(message.encode('utf-8')).hexdigest()






def generate_h5st(params):
    # 解析原始 body
    body_str = params['body']
    body = json.loads(body_str) if isinstance(body_str, str) else body_str

    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "page": body['page'],
        "pagesize": body['pagesize'],
        "area": body['area'],
        "source": body['source'],
        "gb": body['gb'],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get('appid', 'www-jd-com'),
        "body": sha_256(json.dumps(boddy, separators=(',', ':'))),
        "clientVersion": params.get('clientVersion', '1.0.0'),
        "client": params.get('client', 'pc'),
        "functionId": params.get('functionId', 'pc_home_feed'),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call('getH5st', a)

    return h5st


headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "referer": "https://www.jd.com/",
    "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "script",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
}
cookies = {
    "b_webp": "1",
    "b_avif": "1",
    "jcap_dvzw_fp": "_HIVLiZCIEZDIcHwXjPU43al5uvsYIwPfs4O6VE9nqABptbi3ASG0seWjzU_o2F4bpTtKrH1VphdN9RwziykSA==",
    "whwswswws": "",
    "__jdu": "1751545604909534518369",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "shshshfpa": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "shshshfpx": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "autoOpenApp_downCloseDate__jd_czlogin_openapp_": "1752224419174_1",
    "b_dw": "1912",
    "b_dh": "954",
    "b_dpr": "1",
    "commonAddress": "0",
    "regionAddress": "1%2C72%2C55674%2C0",
    "mt_xid": "V2_52007VwMUVl1cV14YSBxUBG8HEFdZWFtYGEEpXVBjCkcCCAhOWBsdGUAAZQAUTg1eVFMDHhoLDDdQEQBeXFBdL0oYXwF7AhROXVhDWhlCHFQOZwMiUG1YYlMfSB1eBmMEFFJcX1ZZGk4RWQ1XAxFRXw%3D%3D",
    "warehistory": '"100124552423,10092745719292,100124552423,100126228944,"',
    "autoOpenApp_downCloseDate_autoOpenApp_autoPromptly": "1752227819923_1",
    "autoOpenApp_downCloseDate_jd_homePage": "1752462226750_1",
    "autoOpenApp_downCloseDate_auto": "1752462295553_1800000",
    "areaId": "19",
    "pt_st": "1_QNKa_p1pyRBuRGl7XqjS4ZLHq52uvHozXQu0yZZJaZBykE4huAxadU8rhTxkOed5v-IuGcuXOYsk2aIeXaDCbyBsLjrt7MdvOSvBodXxczB1FkZgSNPx2J-jeoajHnfW39S6QiYrPFEIK1rDtYq4I4SRP4BxLa81ywRy9N0XGlJp_KtCaUMwl_dWLrRUp5syXYAGm0trSW0tTUQiv77qSqdfTbhMefstFJyB3qwZ",
    "ipLoc-djd": "19-1607-4773-62123",
    "TrackID": "1oewTDv6eVflY_HJgkzuOTgmL_p-6JTPedCSo2I6Yy9OP7jfY6j_peKkFgRgw_yDoqoH1X4N6DllRX6TMyXVIr08DQYKqcjsiluMfFrdiN_uVhlZAsFX03PfWDdQl1ekr",
    "light_key": "AASBKE7rOxgWQziEhC_QY6yaqwEwBx7Yhv4MRkyHQBTrkNpmCoAYLd7HlAkyqlzYYBM-VDyf",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE",
    "__jda": "76161171.1751545604909534518369.1751545604.1752832750.1753068550.13",
    "__jdc": "76161171",
    "__jdv": "76161171|direct|-|none|-|1753068549992",
    "thor": "2B3FAB022DDA7FFF772855629D82E8BA86B6B654FC776E1930AA7FEB1EB3100E27F9030B3D4540E35266CF0E79CE9576D74096EDF955D68A9C844609593272D3E6834972B0996CEE32BFCC132C9D730EE827857749C201C4AEFA976A69C5AE0E19D7C933739AC37D1821596DBF6E43BA71A5FE9B2D0DC0EDB0DE311DBF59A5FFF5045101AE792DEA1544534894270E2392F991B87E4ED2D11A2338D9EAE26C6C",
    "mail_times": "2%2C1",
    "cn": "12",
    "PCSYCityID": "CN_440000_440300_0",
    "shshshfpb": "BApXSxcsPKP1AYEqgcJFHsyDPBX7ULrZhBhMVUQdu9xJ1MppMe462",
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMYFMK6VIIAAAAAC7DFGFTMGATUJ4X",
    "__jdb": "76161171.5.1751545604909534518369|13.1753068550",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17U4bPuaOn-gl8R0m14qVPc7nc7Ev9R1a12W46EiA-rxtKJgaTfE338C2Mzfnf-USBJsydG2SzZd3w3KbOQSsIe3mbOS8gVchpFFp6-xsw2w",
    "flash": "3_sRv_Iyb7mGkKKYqKZ7N3gkF0568VF0vajwL3jklPAeoNa1NxdG3aqyaz6h3WR02hXweUOi5z0hSfSX0j6qZ2xRirF2KjZK_0wH5ke3t3RCKQkxmnS5LgyN9iys5TexBQMTpKE8AhCActtA6SoqGUmvWYfp4Hq6b6OMFhY2GV6jpqaqzhFEn9hV**",
}
url = "https://api.m.jd.com/"
# 1023
params = {
    "h5st": "20250721151554655;pwpiz39dg0033301;b5216;tk05w389f740341lMXgxQURUcEROsmei_i7i7uriHdYg7GXU_RnSuueuWAM53s7UaJ6T6mLYPhpZMpJW4u7W7i4WNdLW;362f129013d8be72e3d6355c14223a1a;5.1;1753082148655;ri_uxFOm9u7i7iri_SoV1iLQFt3R_lsm0msSIlsmOGuj8qrm0mMTLhImOuMsCmMmOGLm_VqTHlYV3lsmOGujMaYi1S7h1SrVNdLWIVIh3SbiKVYi2arVLVLV6Wri6aYhMuMgMiXW41YWLlsmOGujMOLj92ch4xZVCJIVPZrUMuMgMWHmOuMsCm8XClrUEJ6a1uLmOGLmBxoVApISMusmk_Mm8iLTFRJmOGLmItHmOuMsC6nmOGOiOGLm9qbRMlsmOusmk_Mi9uMgMubi5lImOusmOGuj26sm0mMi9aHWMusmOuMsCmMcXVbW86Id5qaUtlsm0mcT-dITNlHmOusmOGuj_uMgMObRMlsmOusmk_siOGLm3aHWMusmOuMsCmLiOGLm4aHWMusmOuMsCurm0mch5lImOusmOGuj_uMgMebRMlsmOusmk_sh7uMgMibRMlsmOusmk_Mm62ciAuLmOGLm9aHWMusmOuMsCurm0m8U3lsmOusmk_chOGLm79ImOusmOGuj_uMgM_ImOusmOGuj_uMgMe4RMusmOuMsztMgMeITJdnQJlsmOGujxtsmkm8WINbh2q4h7u7WNZ4hPdIUMuMgMmrSMusmOuMsztMgMunSMusmk_Mm6WrQOCrh42YUXt8g_2si9usZgt8S3xoVAJ4ZMuMgMqYR7lsmOG_Q;8e3ba565f7466f18b3b5f8300d49274f;tenjKJKT-JoRL1YRI9MT-J4S8ZIZ61YVF94WCeHTJJoTL9cQKxIWCeYU_tXW",
    "appid": "www-jd-com",
    "body": '{"page":1,"pagesize":25,"area":"19_1607_4773_62123","source":"pc-home","gb":1}',
    "clientVersion": "1.0.0",
    "client": "pc",
    "functionId": "pc_home_feed",
    "t": "1753069779715",
    "uuid": "76161171.1751545604909534518369.1751545604.1752832750.1753068550.13",
    "loginType": "3",
    "x-api-eid-token": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMYFMK6VIIAAAAAC7DFGFTMGATUJ4X",
    "callback": "jsonpMore2Goods",
    "_": "1753069779724",
}
h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params['h5st'] = h5st
response = requests.get(url, headers=headers, cookies=cookies, params=params)

print(response.text)
print(response)
